<template>
  <view class="content">
    <view class="topBox">
      <view class="title">
        空土地查询
      </view>
      <view class="main">
        <view class="itemBox">
          <view
              :class="item.isClick?'item2':'item'"
              v-for="item in list"
              @click="handleClickLand(item)"
              :key="item.id"
             v-if="item.status===0"
			        :id="item.name"
          >
		   <!-- v-if="item.status===0" -->
            {{ item.name }}
          </view>
        </view>
      </view>
    </view>
    <view class="bottomBox">
      <view class="title">

      </view>
      <view class="main">
        <uni-calendar
            class="uni-calendar--hook"
            :selected="selected"
            :showMonth="true"
            @change="handleDateChange"
            :range="true"
            @monthSwitch="handleDateChange"
            @monthChoose="handleMonthChoose"
            :startDate="this.opingData.startDate"
            :endDate="this.opingData.endDate"
        />
      </view>
    </view>
    <view class="opBtn">
      <button @click="handleClickBtn">确认预约</button>
    </view>
  </view>
</template>

<script>
import { getLand, getNoEmpty,reserveLand } from "../../request/api";

export default {
  data () {
    return {
      opingData: {
        landId: '',
        date:'',
        dateRange:{
          startDate:'',
          endDate:'',
          allDate:[]
        }
      },
      list: [],
      selected: []
    }
  },
  onShow () {
    this.init()
  },
  methods: {
    init () {
      this.getLand()
    },
    getNoEmpty () {
      if(!this.opingData.landId){
        uni.showToast({
          title:'请先选择土地',
          icon:'none'
        })
        return
      }
      getNoEmpty({
        date: this.opingData.date,
        landId: this.opingData.landId
      }).then(res => {
        if (res.data.code === 200) {
          let data = Array.from(res.data.data)
          this.selected.splice(0)
          data.forEach(item => {
            this.selected.push({
              date: item,
            })
          })
        } else {
          uni.showToast({
            title: res.data.message,
            icon: 'error'
          })
          this.selected.splice(0)
        }
      })
    },
    getLand () {
		const sendInfo={
			type:uni.getStorageSync('type')
			
		}
      getLand(sendInfo).then(res => {
		console.log(res.data.data.records);
        let data = Array.from(res.data.data.records)
        data.forEach(item=>{
          this.list.push({
            ...item,
            isClick:false
          })
        })
		console.log(this.list);
        this.getNoEmpty()
      })
    },
    handleClickLand (props) {
      this.opingData.landId = props.id
      this.list.forEach((item, index) => {
        if(props.id===item.id){
          item.isClick=true
        }else {
          item.isClick=false
        }
      })
      this.getNoEmpty()
    },
    handleDateChange (e) {
      if(e.range.after&&e.range.before){
        let date1 = new Date(e.range.before)
        let date2 = new Date(e.range.after)
        if(date1>date2){
          this.opingData.dateRange.startDate=date2.getUTCFullYear().toString()+'-'+(date2.getUTCMonth()+1)+'-'+date2.getUTCDate()
          this.opingData.dateRange.endDate=date1.getUTCFullYear().toString()+'-'+(date1.getUTCMonth()+1)+'-'+date1.getUTCDate()
        }else {
          this.opingData.dateRange.startDate=date1.getUTCFullYear().toString()+'-'+(date1.getUTCMonth()+1)+'-'+date1.getUTCDate()
          this.opingData.dateRange.endDate=date2.getUTCFullYear().toString()+'-'+(date2.getUTCMonth()+1)+'-'+date2.getUTCDate()
        }
        this.opingData.dateRange.allDate=e.range.data
      }else {
        this.opingData.dateRange.startDate=''
        this.opingData.dateRange.endDate=''
        this.opingData.dateRange.allDate=[]
      }
      this.opingData.date = e.year + '-' + e.month.toString().padStart(2, '0')
      this.getNoEmpty()
    },
    handleMonthChoose(e){
      this.opingData.date = e.year + '-' + e.month.toString().padStart(2, '0')
      this.getNoEmpty()
    },
    handleClickBtn(){
      if(!this.opingData.landId){
        uni.showToast({
          title:'请先选择土地',
          icon:'none'
        })
        return
      }
      if(!this.opingData.dateRange.startDate){
        uni.showToast({
          title:'请选择开始日期',
          icon:'none'
        })
        return
      }
      if(!this.opingData.dateRange.endDate){
        uni.showToast({
          title:'请选择结束日期',
          icon:'none'
        })
        return
      }

      reserveLand({
        beginTime:this.opingData.dateRange.startDate,
        endTime:this.opingData.dateRange.endDate,
        landId:this.opingData.landId
      }).then(res=>{
        if(res.data.code===200){

          this.getNoEmpty()
		  uni.switchTab({
		  	url:'/pages/activity/activity'
		  })
		  uni.showToast({
		    title:'预约成功'
		  })
        }else {
          uni.showToast({
            title:res.data.message,
            icon:'error'
          })
        }
      })
    }
  }

}

</script>

<style scoped lang="less">
.success {
  background-color: #6a6a6a;
}

.content {
  height: 100vh;
  width: 100vw;
  background-color: rgb(225, 225, 225);

  .topBox {
    display: inline-block;
    background-color: rgba(255, 255, 255, 1);
    min-width: 96vw;
    margin-left: 2vw;
    margin-right: 2vw;
    margin-top: 40rpx;
	border-radius: 10px;
	box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
    .title {
      background-color: rgb(82, 159, 135);
      color: rgba(255, 255, 255, 1);
      height: 60rpx;
      line-height: 60rpx;
      text-indent: 10rpx;
    }

    .main {
      overflow-y: auto;
      height: 280rpx;
	  // box-shadow: rgba(50, 50, 93, 0.25) 0px 30px 60px -12px inset, rgba(0, 0, 0, 0.3) 0px 18px 36px -18px inset;

      .itemBox {
        display: flex;
        margin-top: 25rpx;
        margin-bottom: 25rpx;
        justify-items: center;
        flex-wrap: wrap;

        .item {
          text-align: center;
          padding-left: 15rpx;
          padding-right: 15rpx;
          margin: 15rpx;
          min-width: 100rpx;
          min-height: 50rpx;
          line-height: 50rpx;
          border: rgba(0, 0, 0) solid 1rpx;
          border-radius: 5rpx;
        }
        .item2{
          text-align: center;
          padding-left: 15rpx;
          padding-right: 15rpx;
          margin: 15rpx;
          min-width: 100rpx;
          min-height: 50rpx;
          line-height: 50rpx;
          border: rgba(0, 0, 0) solid 1rpx;
          border-radius: 5rpx;
          background-color: rgba(200, 195, 190, 1.0);
        }
      }
    }
  }

  .bottomBox {
    margin-top: 80rpx;
    margin-left: 2vw;
    margin-right: 2vw;

  }

  .opBtn {
    margin-top: 80rpx;
    display: flex;
    justify-content: center;

    button {
      display: inline-block;
      width: 250rpx;
      background-color: rgb(82, 159, 135);
    }
  }
}
</style>
